﻿package  wibrst.utils{
	import flash.utils.ByteArray;
	/**
	 * ...
	 * @author wibrst@qq.com
	 */
	public class StringUtil {
		
		public static function trim(input:String):String{
			return StringUtil.ltrim(StringUtil.rtrim(input));
		}
		
		public static function ltrim(input:String):String{
			var size:Number = input.length;
			for(var i:Number = 0; i < size; i++) if(input.charCodeAt(i) > 32) return input.substring(i);
			return "";
		}
		
		public static function rtrim(input:String):String{
			var size:Number = input.length;
			for(var i:Number = size; i > 0; i--) if(input.charCodeAt(i - 1) > 32) return input.substring(0, i);
			return "";
		}
		
		static public function checkPN( sPn:String ):Boolean {
			var REGEXP_PHONE:RegExp = /1[358]\d{9}/;
			return sPn.match( REGEXP_PHONE ) != null;
		}
		
		static public function getStringsId( aStringsId:Array, sDelimiter:String = "+" ):String {
			aStringsId.sort();
			return aStringsId.join(sDelimiter);
		}
		
		static public function getStringsIdVc( aStringsId:Vector.<String>, sDelimiter:String = "+" ):String {
			aStringsId.sort( sortVcString );
			return aStringsId.join(sDelimiter);
		}
		
		static private function sortVcString( a:*, b:* ):int {
			if ( a < b )  return 1;
			else if ( a > b )  return - 1;
			else return 0;
		}
		
		static public function sortAlphabet( str:String ):String{
			var aStr:Array = str.split( '' );
			aStr.sort();
			return aStr.join('');
		}
		
		static public function getBrief( sSrc:String , 
				nHeadLen:Number = 600, nTailLen:Number = 600, nTolerance:Number = 20, bLineBreaks:Boolean = true ):String {
			if( sSrc.length <= nHeadLen + nTailLen + nTolerance )return sSrc;
			else
			return bLineBreaks ?
					sSrc.slice( 0, sSrc.slice( 0, nHeadLen ).lastIndexOf("\n") )
					+"\n\n\t... ... ...\n\n"
					+ sSrc.slice( -nTailLen + sSrc.slice( -nTailLen ).indexOf("\n") + 1 )
				:
					sSrc.slice( 0, nHeadLen )
					+"... ..."
					+ sSrc.slice( -nTailLen )
				;
		}
		
		static public function urlencodeGB2312(str:String):String {
			var result:String ="";
			var byte:ByteArray =new ByteArray();
			byte.writeMultiByte(str,"gb2312");
			for (var i:int; i<byte.length; i++) {
				result += escape(String.fromCharCode(byte[i]));
			}
			return result;
		}
		
		static public function uintFilled( nOrig:uint, nDigit:uint ):String {
			var s:String = nOrig.toString();
			for ( var i:uint = s.length; i < nDigit; ++ i ) s = "0" + s;
			return s;
		}
	
		static public function getBaseName( sURL:String ):String {
			if(sURL){
				var aURL:Array = sURL.split("/");
				return aURL[aURL.length - 1];
			}
			return "[NULL STRING]";
		}
		
		static public function getRdmString( iTop:int = 32 ):String{    //初始化
		    var number:Number;
		    var nCase:Number;
			
		    var code:String;
		    var checkCode:String = "";
			
		   for(var i:int=0; i<iTop; i++){
		   	   number = Math.random() *1000;
			   nCase=Math.random();
			
			   if(nCase < .3) code = String.fromCharCode(48+(number % 10));        // 数字0的ASCII码是：48
			   else if(nCase < .66) code = String.fromCharCode(65+(number % 26)) ;        // 字母A的ASCII是： 65
			   else code = String.fromCharCode(97+(number % 26)) ;        // 字母a的ASCII是： 97
			   checkCode += code;
		   }
		   return checkCode;
		}
		
		static public function positiveSlash(string:String):void {
			string.replace( /\\/g, "\\" );
		}
		
	}

}